import { Injectable } from '@angular/core';
import { ConnectionBackend, XHRConnection, XHRBackend, Request, Response, RequestOptions, Http } from '@angular/http';
import { HttpInterceptorService } from "./http.interceptor.service";
@Injectable()
export class HttpXHRBackendService implements ConnectionBackend {
    constructor(private interceptor: HttpInterceptorService, private xhr: XHRBackend) {

    }
    createConnection(request: Request): XHRConnection {
        let interceptor = this.interceptor;
        let req: Request = interceptor.beforeRequest ? interceptor.beforeRequest(request) : request;

        let result = this.xhr.createConnection(req);
        result.response = interceptor.afterResponse ? interceptor.afterResponse(result.response) : result.response;

        return result;
    }

}

export function httpFactorys(xhr: XHRBackend, requestionOptions: RequestOptions): Http {
    return new Http(xhr, requestionOptions);
}